package com.uit2012.tool;

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * Created by halfcrazy on 2014/5/5.
 */
public class ScheduleParse {
    private static int NORMAL=5;
    private static int SPORTS=4;
    private static String NORMAL_REGEX="(.*)\\s(.*)\\s(.*)\\s(.*)\\s(.*)\\s(.*)";
    private static String SPORTS_REGEX="(.*)\\s(.*)\\s(.*)\\s(.*)\\s(.*)";
    private String types;
    private String course_name;
    private String teacher_name;
    private String classroom;
    private String last_time;
    private String week_kind;
    private Vector week;
    private String feedstr;

    public static int[] range(int start, int length) {
        int[] range = new int[length - start + 1];
        for (int i = start; i <= length; i++) {
            range[i - start] = i;
        }
        return range;
    }
    public ScheduleParse(){
        this.feedstr=null;
        course_name=null;
        teacher_name=null;
        classroom =null;
        week=new Vector();
        last_time=null;
        types=null;
        week_kind=null;
    }
    public void feed(String arg){
        this.feedstr=arg.replaceAll("\\s{1,}", " ");
        this.feedstr=this.feedstr.replaceAll("([A-Za-z]) ([A-Za-z])","$1_$2");
        course_name=null;
        teacher_name=null;
        classroom =null;
        week=new Vector();
        last_time=null;
        types=null;
        week_kind=null;
    }
    public void parse(){
        int backspace_cnt = 0;
        for(int j=0;j<feedstr.length();j++) {
            if (feedstr.charAt(j) == ' ')backspace_cnt++;
        }
        if(backspace_cnt==NORMAL){
            types="正常";
            Matcher m= Pattern.compile(NORMAL_REGEX, Pattern.UNICODE_CASE).matcher(feedstr);
            if(m.find()) {
                course_name=m.group(1);
                teacher_name=m.group(2);
                classroom =m.group(3);
                last_time=m.group(6);
                week_kind=m.group(5);
                int flag;
                if(week_kind.equals("单周")){
                    flag=1;
                }else if(week_kind.equals("双周")){
                    flag=2;
                }else {
                    flag=3;
                }
                String tmpwk=m.group(4);
                parseWeek(tmpwk,flag);
            }
        }else if(backspace_cnt==SPORTS){
            types="体育";
            Matcher m=Pattern.compile(SPORTS_REGEX,Pattern.UNICODE_CASE).matcher(feedstr);
            if(m.find()) {
                course_name=m.group(1);
                teacher_name=m.group(2);
                last_time=m.group(5);
                week_kind=m.group(4);
                int flag;
                if(week_kind.equals("单周")){
                    flag=1;
                }else if(week_kind.equals("双周")){
                    flag=2;
                }else {
                    flag=3;
                }
                String tmpwk=m.group(3);
                parseWeek(tmpwk,flag);
            }
        }
    }
    public void parseWeek(String tmpwk,int flag){
        tmpwk=tmpwk.substring(0,tmpwk.length()-1);
        String[] tmparr=tmpwk.split("\\.");
        for(int it=0;it<tmparr.length;it++) {
            if(tmparr[it].contains("-")){
                String [] tmprange=tmparr[it].split("-");
                int start=Integer.parseInt(tmprange[0]);
                int end=Integer.parseInt(tmprange[1]);
                int [] rg=range(start,end);
                for(int item=0;item<rg.length;item++)
                    week.add(rg[item]+"");
            }else{
                week.add(tmparr[it]);
            }
        }
        for(int it=0;it<week.size();it++){
            String wk= (String)week.get(it);
            if(flag!=3){
                if(flag==1){//单周
                    if(Integer.parseInt(wk)%2==0){//去除双周
                        week.remove(wk);
                    }
                }else if(flag==2){//双周
                    if(Integer.parseInt(wk)%2==1) {//去除单周
                        week.remove(wk);
                    }
                }
            }
        }
    }
    public String getTypes(){
        return types;
    }
    public String getCourse_name(){
        return course_name;
    }
    public String getTeacher_name(){
        return teacher_name;
    }
    public String getClassroom(){
        return classroom;
    }
    public String getWeek_kind(){
        return week_kind;
    }
    public String getLast_time(){
        return last_time;
    }
    public Vector getWeek(){
        return week;
    }
}
